import random
import copy

class PCB:
    def __init__(self, ID, time, priority):
        self.ID=ID
        self.time=time
        self.priority=priority

usedTime=0

def run(process, t):
    global usedTime
    if t>=process.time:
        print(process.ID,'run',process.time)
        usedTime+=process.time
        process.time=0
        return True
    else:
        process.time-=t
        print(process.ID,'run',t)
        usedTime+=t
        return False

timeSlice=10

def FCFS(PCBList):
    global timeSlice
    while True:
        i=0
        while i<len(PCBList):
            ret=run(PCBList[i], timeSlice)
            if ret:
                del PCBList[i]
            else:
                i+=1
        if len(PCBList)==0:
            break

def highPriority(PCBList):
    sorted(PCBList, key=lambda p: p.priority)
    for process in PCBList:
        run(process, process.time)
    PCBList=[]

def hybrid(PCBList):
    group={}
    for i in range(9):
        group[i]=[]
    for process in PCBList:
        group[process.priority].append(process)

    for i in range(9):
        FCFS(group[i])
    PCBList=[]

PCBList=[]
for i in range(50):
    time=random.randint(1,20)
    priority=random.randint(0,8)
    PCBList.append(PCB(i,time,priority))
PCBListCopy=copy.deepcopy(PCBList)

FCFS(PCBList)
print('used Time:', usedTime)
print()
usedTime=0
PCBList=copy.deepcopy(PCBListCopy)

highPriority(PCBList)
print('used Time:', usedTime)
print()
usedTime=0
PCBList=copy.deepcopy(PCBListCopy)

hybrid(PCBList)
print('used Time:', usedTime)